package queue;

import java.util.Scanner;

/**
 * @author huangxin
 * @date 2019/6/19-19:23
 */
public class CircleQueueTest {

    public static void main(String[] args) {
        CircleArray arrayQueue = new CircleArray(4);
        char key = ' ';
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;

        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列的头数据");
            key = scanner.next().charAt(0);
            switch (key) {
                case 's':
                    arrayQueue.showQueue();
                    break;

                case 'a':
                    System.out.print("输入一个数:");
                    int value = scanner.nextInt();
                    arrayQueue.addQueue(value);
                    break;
                case 'g':
                    try {
                        int result = arrayQueue.getQueue();
                        System.out.printf("取出的数据是%d\n",result);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int result =    arrayQueue.headQueue();
                        System.out.printf("队列头的数据是%d\n",result);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop=false;
                    break;
                default:
                    break;
            }

        }
        System.out.println("程序退出");
    }
}

class CircleArray {
    private int[] arr;
    private int maxSize;
    private int front;//队列头
    private int rear;//队列尾


    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isFill() {
        //maxSize = 4
        //rear = 1
        //front = 2
//        return (rear+maxSize-front+1)%maxSize==0;
        return (rear + 1) % maxSize == front;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public void addQueue(int ele) {
        if (isFill()) {
            System.out.println("队列已满,不能插入");
            return;
        }
        arr[rear] = ele;
        rear = (rear + 1) % maxSize;
    }

    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能获取");
        }
        int result = arr[front];
        front = (front + 1) % maxSize;
        return result;
    }

    public void showQueue(){
        if (isEmpty()) {
            System.out.println("队列为空");
        }
        for (int i = front; i <front+ size(); i++) {
            System.out.printf("arr[%d]=%d\n", i%maxSize, arr[i%maxSize]);
        }
    }

    public int headQueue(){
        if (isEmpty()) {
            throw new RuntimeException("队列为空,不能获取");
        }
        return arr[front];
    }

    public int size(){
        //maxSize = 4
        //rear = 3
        //front = 0
        return (rear-front+maxSize)%maxSize;
    }
}
